package com.subsequent.controller;

import com.subsequent.pojo.ScheduleRules;
import com.subsequent.service.ScheduleRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/scheduleRules")
public class ScheduleRulesController {
    @Autowired
    private ScheduleRulesService scheduleRulesService;

    // 批量新增排班规则
    @PostMapping("/add")
    public String addScheduleRules(@RequestBody List<ScheduleRules> rulesList) {
        Date now = new Date();
        for (ScheduleRules rule : rulesList) {
            rule.setCreatedAt(now);
            rule.setUpdatedAt(now);
            // 设置默认时间段
            if (rule.getTimeSlot() == null) {
                rule.setTimeSlot("08:00-12:00"); // 默认上午时间段
            }
            // 设置默认最大预约数
            if (rule.getMaxAppointments() == null) {
                rule.setMaxAppointments(20); // 默认最大预约数
            }
            // 设置默认是否可用
            if (rule.getIsAvailable() == null) {
                rule.setIsAvailable(0); // 默认可用
            }
        }
        boolean result = scheduleRulesService.saveBatch(rulesList);
        return result ? "success" : "fail";
    }
    @PutMapping("/edit")
    public String editScheduleRule(@RequestBody ScheduleRules scheduleRule) {
        scheduleRule.setUpdatedAt(new Date()); // 更新时间
        boolean result = scheduleRulesService.updateById(scheduleRule);
        return result ? "success" : "fail";
    }
}